 //
//  HDCFWisdomController.m
//  HaoDeCaiFu
//
//  Created by 郑开宏 on 2017/10/28. 
//  Copyright © 2017年 郑开宏. All rights reserved.
//

#import "HDCFWisdomController.h"
#import "HDCFWisdomRiskTestVc.h"
#import "HDCFLoginVc.h"
#import "GeneralMagager.h"
#import "HDCFMakeUpTableViewController.h"
#import "HDCFWisdomTestResultViewController.h"

@interface HDCFWisdomController () <UIAlertViewDelegate>

@property (nonatomic, strong) NSDictionary *dict;

@property (nonatomic, assign) BOOL isServceBad;

@end

@implementation HDCFWisdomController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _isServceBad = NO;
    self.view.backgroundColor = ZKcolor(243, 243, 243);
    self.navigationController.navigationBarHidden=NO;
    UIImageView *headerImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenWidth/720*345)];
    headerImageView.image=[UIImage imageNamed:@"homeBanner"];
    [self.view addSubview:headerImageView];
    
    UIImage*kpImage=[UIImage imageNamed:@"productIntroduce"];
    UIScrollView*scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(W_RATIO(50),headerImageView.bottom-50 + 20 + 20 + 10, screenWidth-W_RATIO(100),screenHeight-headerImageView.height+50-49-64-66 - 50)];
    scrollView.showsVerticalScrollIndicator=NO;
    scrollView.contentSize=CGSizeMake(screenWidth-W_RATIO(100),kpImage.size.height);
    
    
    UIImageView *detailImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0,0, scrollView.width,kpImage.size.height)];
    detailImageView.image=kpImage;
    [scrollView addSubview:detailImageView];
    [self.view addSubview:scrollView];
    
    UIView *whiteView=[[UIView alloc]initWithFrame:CGRectMake(0, scrollView.bottom, screenWidth, 66)];
    whiteView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:whiteView];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(W_RATIO(220), W_RATIO(20), screenWidth-W_RATIO(440), 66-W_RATIO(40));
    [btn setBackgroundImage:[UIImage imageNamed:@"button_kqzt"] forState:UIControlStateNormal];
    [btn setTitle:@"开启智能投顾" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.titleLabel.font=[UIFont systemFontOfSize:15];
    [whiteView addSubview:btn];
    [btn addTarget:self action:@selector(jump) forControlEvents:UIControlEventTouchUpInside];
    
}



- (void)getPersonInfo{
    
    [HttpRequest sendTwoSelfGetRequestWithURL:@"api/User/GetUser" WithParamets:nil success:^(id result) {
        if ([result[@"code"] isEqual:@1]) {
            _isServceBad = NO;
            [SVProgressHUD dismiss];
            self.dict = result[@"result"];
        }else{
            [SVProgressHUD showInfoWithStatus:result[@"msg"]];
            [SVProgressHUD dismissWithDelay:1.0];
        }
    } failure:^(NSError *errer) {
        _isServceBad = YES;
        [SVProgressHUD showInfoWithStatus:@"服务器错误"];
        [SVProgressHUD dismissWithDelay:1.0];
    }];
}

- (void)jump{
    
    if (_isServceBad == YES) {
        [SVProgressHUD showInfoWithStatus:@"服务器错误"];
        [SVProgressHUD dismissWithDelay:1.0];
        return;
    }
    
    NSString *token = kToken;
    if (!(token.length > 0)) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请先登录！" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            HDCFLoginVc *loginVc = [[HDCFLoginVc alloc] init];
            [self.navigationController pushViewController:loginVc animated:NO];
            
        }];
        UIAlertAction * action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alert addAction: action1];
        [alert addAction: action2];
        [self presentViewController:alert animated:NO completion:nil];
        return;
    }
    
    
    if ([self.dict[@"FirstDesc"] isKindOfClass:[NSNull class]]) {
        HDCFWisdomRiskTestVc *tVc = [[HDCFWisdomRiskTestVc alloc] init];
        [self.navigationController pushViewController:tVc animated:YES];
    }else{
        HDCFWisdomTestResultViewController *vc = [[HDCFWisdomTestResultViewController alloc] init];
        vc.riskType = self.dict[@"FirstDesc"];
        vc.style = self.dict[@"FirstDesc"];
        vc.isFromHome = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationItem setTitle:@"智能投顾"];
    
//    [HttpRequest sendSelfGetRequestWithURL:@"file/VersionInfo.json" WithParamets:nil success:^(id result) {
//        NSDictionary *dataDict = result[@"IOS"];
//        //先获取当前工程项目版本号
//        NSDictionary *infoDic=[[NSBundle mainBundle] infoDictionary];
//        NSString *currentVersion = infoDic[@"CFBundleShortVersionString"];
//        if ([currentVersion isEqualToString:dataDict[@"Version"]]) {
            if (kToken) {
                [self getPersonInfo];
            }
//            return ;
//        }else {
//            if ([dataDict[@"Required"] integerValue] == 1) {//强制更新
//                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:dataDict[@"Remark"] message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"更新", nil];
//                alert.delegate = self;
//                alert.tag = 100;
//                [alert show];
//            }else {
//                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:dataDict[@"Remark"] message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"更新", nil];
//                alert.delegate = self;
//                alert.tag = 200;
//                [alert show];
//            }
//        }
//    } failure:^(NSError *errer) {
//        [SVProgressHUD showInfoWithStatus:@"服务器错误"];
//        [SVProgressHUD dismissWithDelay:1.0];
//    }];
}

//alertView 代理
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (alertView.tag == 100) {
        NSURL *url3 = [NSURL URLWithString:@"https://itunes.apple.com/cn/app/xxx/idxxx?l=en&mt=x"];
        [[UIApplication sharedApplication] openURL:url3];
    }else {
        if (buttonIndex == 0) {
            if (kToken) {
                [self getPersonInfo];
            }
        }else {
            NSURL *url3 = [NSURL URLWithString:@"https://itunes.apple.com/cn/app/xxx/idxxx?l=en&mt=x"];
            [[UIApplication sharedApplication] openURL:url3];
        }
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
